package objetos;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class Estatistica {
	
	private String URL = "jdbc:mysql://127.0.0.1:3306/test";
	private String USE = "root";
	private String PASS = "";
	
	public ArrayList<EstatisticaBean> getEstatisticas ()
	{
		ArrayList<EstatisticaBean> lista = new ArrayList<EstatisticaBean>();
		EstatisticaBean bean =  new EstatisticaBean();
		
		String driver = "com.mysql.jdbc.Driver";
		Connection con;
		try {
			Class.forName(driver);
			con = DriverManager.getConnection(URL, USE, PASS);
			Statement stmt = con.createStatement();
			Statement st = null;
			ResultSet rs = null;
			st = con.createStatement();
			// seleciona id
			String sql = "SELECT PALAVRA AS PALAVRA, NOME AS NOME, HISTORICO AS HISTORICO, ERROS AS ERROS, ACERTOS AS ACERTOS, CHANCES AS CHANCES, DICAS AS DICAS, NIVEL AS NIVEL, STATUS AS STATUS FROM ESTATISTICAS_FORCA;";

			// Autenticar
			st = con.createStatement();
			rs = st.executeQuery(sql);

			while (rs.next()) {
				bean = new EstatisticaBean();
				bean.setAcertos(rs.getString("ACERTOS"));
				bean.setChances(rs.getString("CHANCES"));
				bean.setDic(rs.getString("DICAS"));
				bean.setErros(rs.getString("ERROS"));
				bean.setHist(rs.getString("HISTORICO"));
				bean.setNivel(rs.getString("NIVEL"));
				bean.setNome(rs.getString("NOME"));
				bean.setPalavras(rs.getString("PALAVRA"));
				bean.setStatus(rs.getString("STATUS"));
				lista.add(bean);
			}

//			sql = "";
//			// con.commit();
////			stmt.executeUpdate(sql);

			con.close();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}
		
		
		return lista;
	}
	
	public void inserirEstatistica(EstatisticaBean estat) {
		String driver = "com.mysql.jdbc.Driver";
		Connection con;
		boolean res = false;

		try {
			Class.forName(driver);
			con = DriverManager.getConnection(URL, USE, PASS);
			Statement stmt = con.createStatement();

			String sql = "INSERT INTO ESTATISTICAS_FORCA (NOME, PALAVRA, HISTORICO, ERROS, ACERTOS, CHANCES, DICAS, NIVEL, STATUS) VALUES "
					+ "('"
					+ estat.getNome()
					+ "','"
					+ estat.getPalavras()
					+ "','"
					+ estat.getHist()
					+ "','"
					+ estat.getErros()
					+ "','"
					+ estat.getAcertos()
					+ "','"
					+ estat.getChances()
					+ "','"
					+ estat.getDic()
					+ "', '" + estat.getNivel() + "','" + estat.getStatus() + "');";
			// con.commit();
			stmt.executeUpdate(sql);
			res = true;
			con.close();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			res = false;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			res = false;
		}
	}
}
